package com.tm.media.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.tm.media.model.entity.ApArticleContentEntity;
import com.tm.media.model.entity.ArticleEntity;
import com.tm.media.model.entity.NewsEntity;
import com.tm.media.model.entity.StatisticsEntity;
import com.tm.media.model.mapper.ApArticleContentMapper;
import com.tm.media.model.mapper.ArticleMapper;
import com.tm.media.model.mapper.NewsMapper;
import com.tm.media.model.mapper.StatisticsMapper;
import com.tm.media.service.NewsService;
import com.tm.media.utils.utils.PageUtil;
import com.tm.media.utils.utils.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.Date;
import java.util.Locale;

/**
 * @Author Ly
 * @Data 2023/2/22 14:14
 * @ClassName NewsServiceImpl
 */
@Service
public class NewsServiceImpl extends ServiceImpl<NewsMapper, NewsEntity>implements NewsService {
   @Autowired
   private NewsMapper newsMapper;
   @Autowired
   private ArticleMapper articleMapper;
   @Autowired
   private ApArticleContentMapper apArticleContentMapper;
   @Autowired
   private StatisticsMapper statisticsMapper;
    @Override
    public Result newList(PageUtil pageUtil) {

        IPage<NewsEntity> iPage = new Page<NewsEntity>(pageUtil.getPage(),pageUtil.getSize());
        iPage.setCurrent(pageUtil.getPage());
        iPage.setSize(pageUtil.getSize());
        QueryWrapper<NewsEntity> queryWrapper = new QueryWrapper<>();
        if (pageUtil.getChannel_id()!=null){
            queryWrapper.eq("channel_id",pageUtil.getChannel_id());
        }
        if (pageUtil.getStatus()!=null){
            queryWrapper.eq("status",pageUtil.getStatus());
            IPage<NewsEntity> adUserIPage = baseMapper.selectPage(iPage, queryWrapper);
            return Result.SUCCESS(adUserIPage);
        }if (pageUtil.getBegin_pubdate()!=null && pageUtil.getEnd_pubdate()!=null){
            queryWrapper.between("publish_time",pageUtil.getBegin_pubdate(),pageUtil.getEnd_pubdate());
        }
        IPage<NewsEntity> adUserIPage = baseMapper.selectPage(iPage, queryWrapper);
        return Result.SUCCESS(adUserIPage);


    }
    @Override
    public Result submit(NewsEntity newsEntity) {
        Date date1 = null;
        DateFormat df2 = null;
        String oldDate="";
        try {
            oldDate= newsEntity.getPublish_time().replace("Z", " UTC");
            DateFormat df = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSS Z");
            Date date = df.parse(oldDate);
            SimpleDateFormat df1 = new SimpleDateFormat ("EEE MMM dd HH:mm:ss Z yyyy", Locale.UK);
            date1 = df1.parse(date.toString());
            df2 = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        } catch (ParseException e) {
            e.printStackTrace();
        }
        String format = df2.format(date1);

        DateTimeFormatter fmt = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
        LocalDateTime date2 = LocalDateTime.parse(format, fmt);
        /**
         * 获取当前用户的id
         * @return 用户id
         */
        String image="";
        for (int i = 0; i < newsEntity.getImages().size(); i++) {
            if (newsEntity.getImages().size()-1==i){
                image+=newsEntity.getImages().get(i);
            }else {
                image+=newsEntity.getImages().get(i)+"---";
            }
        }
        newsEntity.setImage(image);
        newsEntity.setCreatedTime(LocalDateTime.now());
        newsEntity.setPublishTime(date2);
        newsMapper.insert(newsEntity);
        //新增已发布表
        ArticleEntity articleEntity = new ArticleEntity();
        articleEntity.setArticle_id(newsEntity.getArticleId());
        articleEntity.setAuthorId(newsEntity.getUserId());
        articleEntity.setImages(image);
        articleEntity.setChannelId(newsEntity.getChannel_id());
        articleEntity.setChannelName(newsEntity.getChannelName());
        articleEntity.setTitle(newsEntity.getTitle());
        articleMapper.insert(articleEntity);
        //新增内容表
        ApArticleContentEntity apArticleContentEntity = new ApArticleContentEntity();
        apArticleContentEntity.setContent(newsEntity.getContent());
        apArticleContentEntity.setArticle_id(articleEntity.getId());
        apArticleContentMapper.insert(apArticleContentEntity);
        //自媒体图文数据统计表
        StatisticsEntity statisticsEntity = new StatisticsEntity();
        statisticsEntity.setArticle(articleEntity.getId());
        statisticsMapper.insert(statisticsEntity);
        return Result.SUCCESS(200,"新增成功");
    }

    @Override
    public Result del_news(NewsEntity newsEntity) {
        newsMapper.deleteById(newsEntity.getId());
        return Result.SUCCESS(200,"删除成功");
    }

    @Override
    public Result news(NewsEntity newsEntity) {
        QueryWrapper<NewsEntity> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("id",newsEntity.getId());
        return Result.SUCCESS(  newsMapper.selectOne(queryWrapper)) ;
    }
}
